package com.easytouch.core.ftp;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.TimeZone;

public class MLSxEntryParser extends FTPFileEntryParserImpl {
    private static final MLSxEntryParser PARSER = new MLSxEntryParser();
    private static final HashMap<String, Integer> TYPE_TO_INT = new HashMap();
    private static int[] UNIX_GROUPS;
    private static int[][] UNIX_PERMS;

    public MLSxEntryParser() {
    }

    public FTPFile parseFTPEntry(String entry) {
        String[] parts = entry.split(" ", 2);
        if (parts.length != 2) {
            return null;
        } else {
            FTPFile file = new FTPFile();
            file.setRawListing(entry);
            file.setName(parts[1]);
            String[] facts = parts[0].split(";");
            boolean hasUnixMode = parts[0].toLowerCase(Locale.ENGLISH).contains("unix.mode=");
            String[] arr$ = facts;
            int len$ = facts.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                String fact = arr$[i$];
                String[] factparts = fact.split("=");
                if (factparts.length == 2) {
                    String factname = factparts[0].toLowerCase(Locale.ENGLISH);
                    String factvalue = factparts[1];
                    String valueLowerCase = factvalue.toLowerCase(Locale.ENGLISH);
                    if ("size".equals(factname)) {
                        file.setSize(Long.parseLong(factvalue));
                    } else if ("sizd".equals(factname)) {
                        file.setSize(Long.parseLong(factvalue));
                    } else if ("modify".equals(factname)) {
                        SimpleDateFormat sdf;
                        if (factvalue.contains(".")) {
                            sdf = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
                        } else {
                            sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                        }

                        TimeZone GMT = TimeZone.getTimeZone("GMT");
                        sdf.setTimeZone(GMT);
                        GregorianCalendar gc = new GregorianCalendar(GMT);

                        try {
                            gc.setTime(sdf.parse(factvalue));
                        } catch (ParseException var22) {
                            ;
                        }

                        file.setTimestamp(gc);
                    } else if ("type".equals(factname)) {
                        Integer intType = (Integer)TYPE_TO_INT.get(valueLowerCase);
                        if (intType == null) {
                            file.setType(3);
                        } else {
                            file.setType(intType);
                        }
                    } else if (!factname.startsWith("unix.")) {
                        if (!hasUnixMode && "perm".equals(factname)) {
                            this.doUnixPerms(file, valueLowerCase);
                        }
                    } else {
                        String unixfact = factname.substring("unix.".length()).toLowerCase(Locale.ENGLISH);
                        if ("group".equals(unixfact)) {
                            file.setGroup(factvalue);
                        } else if ("owner".equals(unixfact)) {
                            file.setUser(factvalue);
                        } else if ("mode".equals(unixfact)) {
                            int off = factvalue.length() - 3;

                            for(int i = 0; i < 3; ++i) {
                                int ch = factvalue.charAt(off + i) - 48;
                                if (ch >= 0 && ch <= 7) {
                                    int[] arr$1 = UNIX_PERMS[ch];
                                    int len$1 = arr$1.length;

                                    for(int i$1 = 0; i$ < len$1; ++i$) {
                                        int p = arr$1[i$1];
                                        file.setPermission(UNIX_GROUPS[i], p, true);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return file;
        }
    }

    private void doUnixPerms(FTPFile file, String valueLowerCase) {
        char[] arr$ = valueLowerCase.toCharArray();
        int len$ = arr$.length;

        for(int i$ = 0; i$ < len$; ++i$) {
            char c = arr$[i$];
            switch(c) {
                case 'a':
                    file.setPermission(0, 1, true);
                case 'b':
                case 'f':
                case 'g':
                case 'h':
                case 'i':
                case 'j':
                case 'k':
                case 'n':
                case 'o':
                case 'q':
                case 's':
                case 't':
                case 'u':
                case 'v':
                default:
                    break;
                case 'c':
                    file.setPermission(0, 1, true);
                    break;
                case 'd':
                    file.setPermission(0, 1, true);
                    break;
                case 'e':
                    file.setPermission(0, 0, true);
                    break;
                case 'l':
                    file.setPermission(0, 2, true);
                    break;
                case 'm':
                    file.setPermission(0, 1, true);
                    break;
                case 'p':
                    file.setPermission(0, 1, true);
                    break;
                case 'r':
                    file.setPermission(0, 0, true);
                    break;
                case 'w':
                    file.setPermission(0, 1, true);
            }
        }

    }

    public static FTPFile parseEntry(String entry) {
        return PARSER.parseFTPEntry(entry);
    }

    public static MLSxEntryParser getInstance() {
        return PARSER;
    }

    static {
        TYPE_TO_INT.put("file", 0);
        TYPE_TO_INT.put("cdir", 1);
        TYPE_TO_INT.put("pdir", 1);
        TYPE_TO_INT.put("dir", 1);
        UNIX_GROUPS = new int[]{0, 1, 2};
        UNIX_PERMS = new int[][]{new int[0], {2}, {1}, {2, 1}, {0}, {0, 2}, {0, 1}, {0, 1, 2}};
    }
}

